const app = getApp<IAppOption>()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Component({
    properties: {
        id: Number
    },
    data: {
        lineupList: {},
        userInfo: {
            avatarUrl: defaultAvatarUrl,
            nickName: '',
        },
        hasUserInfo: false,
        canIUseGetUserProfile: wx.canIUse('getUserProfile'),
        canIUseNicknameComp: wx.canIUse('input.type.nickname')
    },
    methods: {
        // 事件处理函数
        menuChange(e: any) {
            // @ts-ignore
            this.setData({active: e.detail});
            switch (e.detail) {
                case 1:
                    wx.navigateTo({
                        url: `../lineup/lineup`
                    })
                    break;
                case 0:
                    wx.navigateTo({
                        url: `../index/index`
                    })
                    break;
            }
        },
        openLineupDetail(e: any) {
            // @ts-ignore
            wx.navigateTo({
                url: `/pages/lineup-detail/lineup-detail?id=${e.target.dataset.id}`,
            })
        },
        onChooseAvatar(e: any) {
            const { avatarUrl } = e.detail
            console.log(avatarUrl)
            // 存储avatarUrl
            const user = wx.getStorageSync('user');
            user.avatarUrl = avatarUrl
            wx.setStorageSync('user', user)

            // 同步存储到后台
            wx.request({
                url:'https://wxapi.db101.cn/v1/jcc/user/update/avatar',
                method: 'POST',
                data: {
                    // @ts-ignore
                    "avatarUrl": avatarUrl,
                    // @ts-ignore
                    "id": user.id
                },
                header: {
                    'content-type': 'application/json' // 默认值
                }
            })
            this.setData({
                "userInfo.avatarUrl": avatarUrl,
                hasUserInfo: avatarUrl !== defaultAvatarUrl,
            })
        },
        onInputChange(e: any) {
            const nickName = e.detail.value
            const { avatarUrl } = this.data.userInfo
            this.setData({
                "userInfo.nickName": nickName,
                hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl
            })
        },
        getUserProfile() {
            // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
            wx.getUserProfile({
                desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
                success: (res) => {
                    console.log(res)
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            })
        },
        openFav() {
            // @ts-ignore
            wx.navigateTo({
                url: `/pages/fav-list/fav-list`,
            })
        }
    },
    lifetimes: {
        attached: function() {
            // @ts-ignore
            // 在组件实例进入页面节点树时执行
            const user = wx.getStorageSync('user');
            if (user.avatarUrl) {
                this.setData({
                    "userInfo.avatarUrl": user.avatarUrl,
                    hasUserInfo: user.avatarUrl !== defaultAvatarUrl,
                })
            }
        },
        detached: function() {
            // 在组件实例被从页面节点树移除时执行
        },
    }
})
